// 螺旋矩阵
class Solution {
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
        vector<int> ret;
        int left = 0, right = matrix[0].size()-1, top = 0, buttom = matrix.size()-1;
        while(true)
        {
            for(int i = left; i <= right; ++i) ret.push_back(matrix[top][i]);
            if(++top > buttom) break;
            for(int i = top; i <= buttom; ++i) ret.push_back(matrix[i][right]);
            if(--right < left) break;
            for(int i = right; i >= left; --i) ret.push_back(matrix[buttom][i]);
            if(--buttom < top) break;
            for(int i = buttom; i >= top; --i) ret.push_back(matrix[i][left]);
            if(++left > right) break;
        }
        return ret;
    }
};

// 560. 和为 K 的子数组
class Solution {
public:
    int subarraySum(vector<int>& nums, int k) {
        unordered_map<int, int> hash;
        hash[0] = 1;
        int sum = 0, ret = 0;
        for(auto e : nums)
        {
            sum += e;
            if(hash.count(sum - k)) ret += hash[sum - k];
            hash[sum]++;
        }
        return ret;
    }
};